var minPlayers = 2;

function init() {
	em.setProperty("state", "0");
	em.setProperty("leader", "true");
}

function setup(level, leaderid) {
	em.setProperty("state", "1");
	em.setProperty("leader", "true");
	var eim = em.newInstance("Pirate" + leaderid);
	em.setProperty("stage2", "0");
	em.setProperty("stage2a", "0");
	em.setProperty("stage3a", "0");
	em.setProperty("stage4", "0");
	em.setProperty("stage5", "0");
	eim.setInstanceMap(925100000).resetPQ(level);
	eim.setInstanceMap(925100100).resetPQ(level);
	var map = eim.setInstanceMap(925100200);
	map.resetPQ(level);
	for (var i = 0; i < 5; i++) {
		var mob = em.getMonster(9300124);
		var mob2 = em.getMonster(9300125);
		var mob3 = em.getMonster(9300124);
		var mob4 = em.getMonster(9300125);
		eim.registerMonster(mob);
		eim.registerMonster(mob2);
		eim.registerMonster(mob3);
		eim.registerMonster(mob4);
		mob.changeLevel(level);
		mob2.changeLevel(level);
		mob3.changeLevel(level);
		mob4.changeLevel(level);
		map.spawnMonsterOnGroundBelow(mob, new java.awt.Point(430, 75));
		map.spawnMonsterOnGroundBelow(mob2, new java.awt.Point(1600, 75));
		map.spawnMonsterOnGroundBelow(mob3, new java.awt.Point(430, 238));
		map.spawnMonsterOnGroundBelow(mob4, new java.awt.Point(1600, 238));
	}
	map = eim.setInstanceMap(925100201);
	map.resetPQ(level);
	for (var i = 0; i < 10; i++) {
		var mob = em.getMonster(9300112);
		var mob2 = em.getMonster(9300113);
		eim.registerMonster(mob);
		eim.registerMonster(mob2);
		mob.changeLevel(level);
		mob2.changeLevel(level);
		map.spawnMonsterOnGroundBelow(mob, new java.awt.Point(0, 238));
		map.spawnMonsterOnGroundBelow(mob2, new java.awt.Point(1700, 238));
	}
	eim.setInstanceMap(925100202).resetPQ(level);
	map = eim.setInstanceMap(925100300);
	map.resetPQ(level);
	for (var i = 0; i < 5; i++) {
		var mob = em.getMonster(9300117);
		var mob2 = em.getMonster(9300118);
		var mob3 = em.getMonster(9300117);
		var mob4 = em.getMonster(9300118);
		eim.registerMonster(mob);
		eim.registerMonster(mob2);
		eim.registerMonster(mob3);
		eim.registerMonster(mob4);
		mob.changeLevel(level);
		mob2.changeLevel(level);
		mob3.changeLevel(level);
		mob4.changeLevel(level);
		map.spawnMonsterOnGroundBelow(mob, new java.awt.Point(430, 75));
		map.spawnMonsterOnGroundBelow(mob2, new java.awt.Point(1600, 75));
		map.spawnMonsterOnGroundBelow(mob3, new java.awt.Point(430, 238));
		map.spawnMonsterOnGroundBelow(mob4, new java.awt.Point(1600, 238));
	}
	map = eim.setInstanceMap(925100301);
	map.resetPQ(level);
	for (var i = 0; i < 10; i++) {
		var mob = em.getMonster(9300112);
		var mob2 = em.getMonster(9300113);
		eim.registerMonster(mob);
		eim.registerMonster(mob2);
		mob.changeLevel(level);
		mob2.changeLevel(level);
		map.spawnMonsterOnGroundBelow(mob, new java.awt.Point(0, 238));
		map.spawnMonsterOnGroundBelow(mob2, new java.awt.Point(1700, 238));
	}
	eim.setInstanceMap(925100302).resetPQ(level);
	eim.setInstanceMap(925100400).resetPQ(level);
	map = eim.setInstanceMap(925100500);
	map.resetPQ(level);
	var mob5 = em.getMonster(9300105);
	var mob6 = em.getMonster(9300106);
	eim.registerMonster(mob5);
	eim.registerMonster(mob6);
	mob5.changeLevel(level);
	map.spawnMonsterOnGroundBelow(mob5, new java.awt.Point(387, 237));
	map.spawnMonsterOnGroundBelow(mob6, new java.awt.Point(387, 237));


	eim.startEventTimer(1200000); //20 mins
	return eim;
}

function playerEntry(eim, player) {
	var map = eim.getMapInstance(0);
	player.changeMap(map, map.getPortal(0));
	player.tryPartyQuest(1204);
}

function playerRevive(eim, player) {
}

function scheduledTimeout(eim) {
	end(eim);
}

function changedMap(eim, player, mapid) {
	if (mapid < 925100000 || mapid > 925100500) {
		eim.unregisterPlayer(player);

		if (eim.disposeIfPlayerBelow(0, 0)) {
			em.setProperty("state", "0");
			em.setProperty("leader", "true");
		}
	}
}

function playerDisconnected(eim, player) {
	return 0;
}

function monsterValue(eim, mobId) {
	return 1;
}

function playerExit(eim, player) {
	eim.unregisterPlayer(player);

	if (eim.disposeIfPlayerBelow(0, 0)) {
		em.setProperty("state", "0");
		em.setProperty("leader", "true");
	}
}

function end(eim) {
	eim.disposeIfPlayerBelow(100, 925100700);
	em.setProperty("state", "0");
	em.setProperty("leader", "true");
}

function clearPQ(eim) {
	end(eim);
}

function allMonstersDead(eim) {
}

function leftParty(eim, player) {
	// If only 2 players are left, uncompletable:
	end(eim);
}
function disbandParty(eim) {
	end(eim);
}
function playerDead(eim, player) { }
function cancelSchedule() { }